import request from "@/js/axios.js";
import {useToast} from "vue-toastification";
import {useScoreStore} from "@/store/userStore.js";



export let getAllScore = (pageNo,pageSize,msg,type) => {
    return request.get('/score/getAllScore?pageNo='+pageNo+'&pageSize='+pageSize+'&msg='+msg+'&type='+type)
        .then((res) => {
            if(res.data.code === 0) {
                console.log(res.data.data);
                return res.data.data;
            }
            throw "获取分数列表出错"
        }).catch((err) => {
            console.log(err);
            return false
        })
}


export let getSemester =  () =>{
    return request.get('/semester/findAllSemesters')
        .then((res) => {
            console.log(res.data)
            return res.data.data;
        })
}

export let getCourse =  () =>{
    return request.get('/course/findAllCourses')
        .then((res) => {
            console.log(res.data)
            return res.data.data;
        })
}

export let addScore =  (score) =>{
    request.post('/score/addScore',score).then((res) => {
        if(res.data.code === 0) {
            useToast().success("成绩录入成功！");
            return
        }
        throw new Error()
    }).catch((err) => {
        console.log(err);
        useToast().error("成绩录入失败",err);
    })
}


export let deleteScore =  (scoreId) =>{
    request.post(`/score/deleteScore?scoreId=${scoreId}`).then(async (res) => {
        if (res.data.code === 0) {
            useToast().success("成绩删除成功！");
            await useScoreStore().updateScores(1, 7)
            return
        }
        throw ''
    }).catch((err) => {
        console.log(err);
        useToast().error("成绩删除失败",err);
    })
}


export let updateScore =  (score) =>{
    console.log('成绩',score)
    request.post('/score/updateScore',score).then((res) => {
        if(res.data.code === 0) {
            useToast().success("成绩更新成功！");
            return
        }
        throw ''
    }).catch((err) => {
        console.log(err);
        useToast().error("成绩更新失败",err);
    })
}

export let getCourseByName =  (name) =>{
    return request.get(`/course/getCourseByName?name=${name}`).then((res) => {
        if(res.data.code === 0) {
            return res.data.data
        }
        throw ''
    }).catch((err) => {
        console.log(err);
        useToast().error("获取课程表失败",err);
        return null
    })
}
